package com.zwz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwz.bean.AdminLoginInfo;
import com.zwz.bean.UserLoginInfo;
import com.zwz.control.util.R;
import com.zwz.dao.UserLoginDao;
import com.zwz.service.UserLoginService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    private UserLoginDao userLoginDao;


    /**
     * 用户注册
     *
     * @param userLoginInfo
     * @return
     */
    @Override
    public UserLoginInfo register(UserLoginInfo userLoginInfo) {
        UserLoginInfo existUser = userLoginDao.findUserByName(userLoginInfo.getUsername());
        if (existUser != null) {
            //如果用户名已存在
            return null;
        } else {
            userLoginDao.regist(userLoginInfo);
            System.out.println(userLoginInfo.getId());
            //将登录id存入用户信息表中
            userLoginDao.addUserInfo(userLoginInfo.getId());

            return userLoginInfo;
        }

    }

    @Override
    public Integer login(UserLoginInfo userLoginInfo) {
        Integer userId = userLoginDao.login(userLoginInfo);
        return userId;
    }

    @Override
    public boolean deleteById(Integer id) {
        return userLoginDao.deleteById(id) == 1 && userLoginDao.deleteUserInfo(id) == 1;
    }

    @Override
    public IPage<UserLoginInfo> getPage(int currentPage, int pageSize, UserLoginInfo userLoginInfo) {
        IPage<UserLoginInfo> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<UserLoginInfo> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(userLoginInfo.getUsername()), UserLoginInfo::getUsername, userLoginInfo.getUsername());
        lqw.orderByDesc(UserLoginInfo::getId);
        userLoginDao.selectPage(page, lqw);
        return page;
    }

}
